package com.demo01.controller;

import com.demo01.mapper.UserMapper;
import com.demo01.pojo.Employee;
import com.demo01.pojo.GuestUser;
import com.demo01.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Controller
public class LoginController {
//    @Autowired
//    UserMapper userMapper;

    @Autowired
    UserService userService;
    /*
    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password ,
                        Model model,
                        HttpSession session){
        if (!StringUtils.isEmpty(username)&&"123456".equals(password)){
            session.setAttribute("loginUser",username);
            return "redirect:/dashboard.html";
        }else {
            model.addAttribute("msg","用户名或密码错误");
            return "sign";
        }
    }*/
    @RequestMapping("/user/login")
    public String login( String username,String password , Model model){
        /**
         * 使用Shiro编写认证操作
         */
        //1.获取Subject
        Subject subject = SecurityUtils.getSubject();
        //2.封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        System.out.println(username+password);
        GuestUser guestUser = userService.findByName(username);
        model.addAttribute("users",guestUser);
        guestUser.getUsername();
        //3.执行登录方法
        try {

            subject.login(token);

            //登录成功
            //跳转到dashboard.html
            //redirect代表重定向请求路径
            return "redirect:/dashboard.html";
        } catch (UnknownAccountException e) {
            //e.printStackTrace();
            //登录失败:用户名不存在
            model.addAttribute("msg", "用户名不存在");
            return "sign";
        }catch (IncorrectCredentialsException e) {
            //e.printStackTrace();
            //登录失败:密码错误
            model.addAttribute("msg", "密码错误");
            return "sign";
        }
    }
    //注销
    @RequestMapping("/user/logout")
    public String logout(HttpSession httpSession){
        httpSession.invalidate();
        return "redirect:/sign.html";
    }
    //注册
    @RequestMapping("/register")
    public String register(){
        return "register.html";
    }

    //添加用户
    @PostMapping("/register")
    public String addreg(GuestUser guestUser) {

        System.out.println("save=>" + guestUser);
        //添加用户
        userService.save(guestUser);
        return "redirect:/sign.html";
    }
    //未授权
    @RequestMapping("/error/noAuth")
    public String noAuth(){
        return "/error/noAuth.html";
    }
}
